/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Login.java
 *
 * Created on 05 29, 11, 10:58:44 AM
 */
package vera;

import vera.hibernate.dao.SystemUsersDAO;
import vera.hibernate.entity.SystemUsers;

/**
 *
 * @author kasia
 */
public class Login extends javax.swing.JFrame {

    /** Creates new form Login */
    public Login() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        m_labelUsername = new javax.swing.JLabel();
        m_textFieldUsername = new javax.swing.JTextField();
        m_labelPassword = new javax.swing.JLabel();
        m_buttonOK = new javax.swing.JButton();
        m_buttonCancel = new javax.swing.JButton();
        m_textFieldPassword = new javax.swing.JPasswordField();
        m_labelVera = new javax.swing.JLabel();
        m_labelError = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(vera.VERAApp.class).getContext().getResourceMap(Login.class);
        m_labelUsername.setText(resourceMap.getString("m_labelUsername.text")); // NOI18N
        m_labelUsername.setName("m_labelUsername"); // NOI18N

        m_textFieldUsername.setText(resourceMap.getString("m_textFieldUsername.text")); // NOI18N
        m_textFieldUsername.setName("m_textFieldUsername"); // NOI18N

        m_labelPassword.setText(resourceMap.getString("m_labelPassword.text")); // NOI18N
        m_labelPassword.setName("m_labelPassword"); // NOI18N

        m_buttonOK.setText(resourceMap.getString("m_buttonOK.text")); // NOI18N
        m_buttonOK.setName("m_buttonOK"); // NOI18N
        m_buttonOK.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                m_buttonOKMouseClicked(evt);
            }
        });

        m_buttonCancel.setText(resourceMap.getString("m_buttonCancel.text")); // NOI18N
        m_buttonCancel.setName("m_buttonCancel"); // NOI18N
        m_buttonCancel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                m_buttonCancelMouseClicked(evt);
            }
        });

        m_textFieldPassword.setText(resourceMap.getString("m_textFieldPassword.text")); // NOI18N
        m_textFieldPassword.setName("m_textFieldPassword"); // NOI18N

        m_labelVera.setFont(resourceMap.getFont("m_labelVera.font")); // NOI18N
        m_labelVera.setText(resourceMap.getString("m_labelVera.text")); // NOI18N
        m_labelVera.setName("m_labelVera"); // NOI18N

        m_labelError.setForeground(resourceMap.getColor("m_labelError.foreground")); // NOI18N
        m_labelError.setText(resourceMap.getString("m_labelError.text")); // NOI18N
        m_labelError.setName("m_labelError"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addComponent(m_labelVera)
                .addContainerGap(899, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(385, 385, 385)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(m_labelError, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 222, Short.MAX_VALUE)
                    .addComponent(m_textFieldPassword, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 222, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(m_buttonOK)
                        .addGap(18, 18, 18)
                        .addComponent(m_buttonCancel))
                    .addComponent(m_labelPassword, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(m_textFieldUsername, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 222, Short.MAX_VALUE)
                    .addComponent(m_labelUsername, javax.swing.GroupLayout.Alignment.LEADING))
                .addGap(393, 393, 393))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addComponent(m_labelVera)
                .addGap(192, 192, 192)
                .addComponent(m_labelUsername)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(m_textFieldUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(m_labelPassword)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(m_textFieldPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(m_buttonOK)
                    .addComponent(m_buttonCancel))
                .addGap(18, 18, 18)
                .addComponent(m_labelError, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(183, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void m_buttonOKMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_m_buttonOKMouseClicked
        String username = m_textFieldUsername.getText();
        String password = new String(m_textFieldPassword.getPassword());
        SystemUsersDAO sysUsersDAO = new SystemUsersDAO();
        SystemUsers user = sysUsersDAO.getUser(username, password);
        System.out.println(user);
        if (user!=null) {
            m_labelError.setText("");
            this.setVisible(false);
            this.dispose();
            new WelcomeScreen().setVisible(true);
        }  else {
            m_labelError.setText(Constants.INVALID_LOGIN);
        }
    }//GEN-LAST:event_m_buttonOKMouseClicked

    private void m_buttonCancelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_m_buttonCancelMouseClicked
        this.setVisible(false);
        this.dispose();
    }//GEN-LAST:event_m_buttonCancelMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Login().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton m_buttonCancel;
    private javax.swing.JButton m_buttonOK;
    private javax.swing.JLabel m_labelError;
    private javax.swing.JLabel m_labelPassword;
    private javax.swing.JLabel m_labelUsername;
    private javax.swing.JLabel m_labelVera;
    private javax.swing.JPasswordField m_textFieldPassword;
    private javax.swing.JTextField m_textFieldUsername;
    // End of variables declaration//GEN-END:variables
}
